package com.yfp.system.api.factory;

import com.yfp.common.core.domain.R;
import com.yfp.common.core.web.domain.AjaxResult;
import com.yfp.system.api.RemoteUserService;
import com.yfp.system.api.domain.SysDictData;
import com.yfp.system.api.domain.SysUser;
import com.yfp.system.api.model.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * 用户服务降级处理
 */
@Component
public class RemoteUserFallbackFactory implements FallbackFactory<RemoteUserService> {
    private static final Logger log = LoggerFactory.getLogger(RemoteUserFallbackFactory.class);

    @Override
    public RemoteUserService create(Throwable throwable) {
        log.error("用户服务调用失败:{}", throwable.getMessage());
        return new RemoteUserService() {
            @Override
            public R<LoginUser> getUserInfo(String username, String source) {
                return R.fail("获取用户失败:" + throwable.getMessage());
            }

            @GetMapping("/user/infoByPhone/{phone}")
            @Override
            public R<LoginUser> getUserInfoByPhone(String phone, String source) {

                return R.fail("获取用户失败:" + throwable.getMessage());
            }

            /**
             * 重置密码
             *
             * @param user 用户信息
             * @return 结果
             */
            @Override
            public AjaxResult resetPwd(SysUser user) {
                return AjaxResult.error("重置密码失败,请稍后再试");
            }

            /**
             * 查询字典数据详细
             *
             * @param dictCode
             */
            @Override
            public R<SysDictData> getDictInfoByDictCode(Long dictCode, String source) {
                return R.fail("查询字典数据详细失败:" + throwable.getMessage());
            }

            @Override
            public R<Long> addUser(SysUser user) {
                return R.fail("团餐审核通过添加用户:" + throwable.getMessage());
            }

            @Override
            public R<Void> updateAvatar(String avatarUrl, String source) {
                return R.fail("修改用户头像失败:" + throwable.getMessage());
            }

            @Override
            public AjaxResult edit(SysUser user) {
                return AjaxResult.error( throwable.getMessage());
            }

            @Override
            public AjaxResult getByUserName(String userName) {
                return AjaxResult.error( throwable.getMessage());
            }

            @Override
            public R<Boolean> registerUserInfo(SysUser sysUser, String source) {
                return R.fail("注册用户失败:" + throwable.getMessage());
            }

            @Override
            public R<Boolean> updateUserInfo(SysUser sysUser, String source) {
                return R.fail("更新用户失败:" + throwable.getMessage());
            }

            /**
             * 通过手机号查询用户信息
             *
             * @param phone  手机号
             * @param source 请求来源
             * @return 结果
             */
            @Override
            public R<LoginUser> getOperationUserInfoByPhone(String phone, String source) {
                return R.fail("获取用户失败,请稍后再试");
            }

            @Override
            public R<LoginUser> getOperationUserInfoByUser(Long userId, String source) {
                return R.fail("获取用户失败,请稍后再试");
            }

            @Override
            public R<LoginUser> getGroupUserInfoByPhone(String phone, String source) {
                return R.fail("获取用户失败,请稍后再试");
            }
        };
    }
}
